package com.lungle.warmdiary.app.view.settings.fragment;

import android.annotation.SuppressLint;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebSettings.PluginState;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;

import com.lungle.warmdiary.R;
import com.lungle.warmdiary.app.base.BaseActivity;
import com.lungle.warmdiary.app.base.BaseFragment;
import com.lungle.warmdiary.app.view.common.SimpleBackActivity;


public class BrowserFragment extends BaseFragment {
    public static final String BROWSER_KEY = "browser_url";
    public static final String BROWSER_TITLE = "browser_title";
    private WebView mWebView;
    private ProgressBar mProgress;
    private String mCurrentUrl;
    private String mTitle;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle bundle = getActivity().getIntent().getBundleExtra(
                SimpleBackActivity.BUNDLE_KEY_ARGS);
        if (bundle != null) {
            mCurrentUrl = bundle.getString(BROWSER_KEY);
            mTitle = bundle.getString(BROWSER_TITLE);
        }
    }

    @SuppressLint("InflateParams")
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_browser, null);
        initView(view);
        initData();
        return view;
    }

    @Override
    public void initView(View view) {
        if (getActivity() instanceof BaseActivity) {
            if (mTitle != null)
                ((BaseActivity) getActivity()).setActionBarTitle(mTitle);
        }
        mWebView = (WebView) view.findViewById(R.id.webview);
        mProgress = (ProgressBar) view.findViewById(R.id.progress);
        initWebview();

    }

    @Override
    public void initData() {
        mWebView.loadUrl(mCurrentUrl);
    }

    @SuppressLint("SetJavaScriptEnabled")
    @SuppressWarnings("deprecation")
    private void initWebview() {
        // cookie = CookieManager.getInstance();
        WebSettings webSettings = mWebView.getSettings();

        //自适应网页
        webSettings.setUseWideViewPort(true);
        webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);

        webSettings.setJavaScriptEnabled(true); // 启用支持javascript
        webSettings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);// 优先使用缓存
        webSettings.setAllowFileAccess(true);// 可以访问文件
        webSettings.setBuiltInZoomControls(true);// 支持缩放
        webSettings.setSupportZoom(true);

        if (android.os.Build.VERSION.SDK_INT >= 11) {
            webSettings.setPluginState(PluginState.ON);
            webSettings.setDisplayZoomControls(false);// 支持缩放
        }

        mWebView.setWebViewClient(new MyWebViewClient());

        mWebView.setWebChromeClient(new MyWebChromeClient());
    }

    private class MyWebChromeClient extends WebChromeClient {
        @Override
        public void onReceivedTitle(WebView view, String title) {
            super.onReceivedTitle(view, title);
        }

        @Override
        public void onReceivedIcon(WebView view, Bitmap icon) {
            super.onReceivedIcon(view, icon);
        }

        @Override
        public void onProgressChanged(WebView view, int newProgress) { // 进度
            super.onProgressChanged(view, newProgress);
            // if (newProgress == 100) {
            // mProgress.setVisibility(View.GONE);
            // } else {
            // mProgress.setVisibility(View.VISIBLE);
            // mProgress.setProgress(newProgress);
            // }
            if (newProgress > 90) {
                mProgress.setVisibility(View.GONE);
            }
        }
    }

    private class MyWebViewClient extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            onUrlLoading(view, url);
            boolean flag = super.shouldOverrideUrlLoading(view, url);
            mCurrentUrl = url;
            return flag;
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
            onUrlFinished(view, url);
        }
    }


    protected void onUrlLoading(WebView view, String url) {
        mProgress.setVisibility(View.VISIBLE);
    }

    protected void onUrlFinished(WebView view, String url) {
        mCurrentUrl = url;
        mProgress.setVisibility(View.GONE);
    }

    @Override
    public void onResume() {
        super.onResume();
        mWebView.onResume();
    }

    @Override
    public void onPause() {
        super.onPause();
        mWebView.onPause();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mWebView.destroy();
    }

}
